def ifile = new File('/home/auerf/temp/identified_proteins_mapped.txt')
def imap = [:]
ifile.each(){
	def line = it.split('\t')
	imap.put(line[1], line[0])
}
def file = new File('/home/auerf/data/Uniprot/proteins_flat.txt')

boolean reviewed = false
def ac = []
def prot = [:]
def curEle = [:]
def curProt = prot
def an = []
curProt.put('AltName', an)
def shor = []
curEle.put('Short', an)
def ec = []
curEle.put('EC', ec)
def con = []
curProt.put('Contains', con)
def inc = []
curProt.put('Includes', inc)
def go = []
curProt.put('GO', go)
def curGene = [:]
def gene = []
curProt.put('GN', gene)
def skip = false

file.eachWithIndex(){line,index->
//	if((index>18000)&&(index<24000)){
	if(true){
//            println line
		if(line.startsWith('ID')){
			reviewed = (line.contains('Reviewed;')) ? true : false
		}else if(line.startsWith('AC')){
			def tm = line[2..-1].replaceAll(' ','').split(';')
                        tm.each(){
                            ac.add(it)
                        }
                }else if(line.startsWith('GN')){
//                    println line[5..-1]
                    (line[5..-1]+' ').split('; ').each(){gn->
                        if(gn.startsWith('Name=')){
//                            println 'Name: '+gn[5..-1]
                            curGene = ['name':gn[5..-1], 'Synonym':[]]
                            prot['GN'].add(curGene)
                        }else if(gn.startsWith('Synonyms=')){
                            gn[9..-1].split(', ').each(){
//                                println 'S: '+it
                                curGene['Synonym'].add(it)
                            }
                        }
                    }
		}else if(line.startsWith('DE')){

			if(line.contains('Contains:')){
				curProt = [:]
				prot['Contains'].add(curProt)
                                def ant = []
                                curProt.put('AltName', ant)
			}else if(line.contains('Includes:')){
				curProt = [:]
				prot['Includes'].add(curProt)
                                def ant = []
                                curProt.put('AltName', ant)
			}

			if(line.contains('RecName: ')){
				curEle = [:]
				curProt.put('ProtName',curEle)
                                shor = []
                                curEle.put('Short',shor)
                                ec = []
                                curEle.put('EC', ec)
				skip = false
			}else if(line.contains('AltName: ')){
				curEle = [:]
				curProt['AltName'].add(curEle)
                                shor = []
                                curEle.put('Short',shor)
                                ec = []
                                curEle.put('EC', ec)
				skip = false
			}else if(line.contains('SubName: ')){
				skip = true
			}else if(line.contains('Flags: ')){
				def tmp = line.split('Flags: ')[1]
                                prot.put('Flags', tmp[0..-2])
			}
			if(! skip){
				if(line.contains('Full=')){
					def tmp = line.split('Full=')[1]
					curEle.put('Full', tmp[0..-2])
				}else if(line.contains('Short=')){
					def tmp = line.split('Short=')[1]
					curEle['Short'].add(tmp[0..-2])
				}else if(line.contains('EC=')){
					def tmp = line.split('EC=')[1]
					curEle['EC'].add(tmp[0..-2])
				}else if(line.contains('Allergen=')){
					def tmp = line.split('Allergen=')[1]
					curEle.put('Allergen', tmp[0..-2])
				}else if(line.contains('Biotech=')){
					def tmp = line.split('Biotech=')[1]
					curEle.put('Biotech', tmp[0..-2])
				}else if(line.contains('CD_antigen=')){
					def tmp = line.split('CD_antigen=')[1]
					curEle.put('CD_antigen', tmp[0..-2])
				}else if(line.contains('INN=')){
					def tmp = line.split('INN=')[1]
					curEle.put('INN', tmp[0..-2])
				}
			}
//                        println line
//                        println prot
		}else if(line.startsWith('DR   GO; ')){
                    def goline = line[9..-2].split('; ')
                    def gotmp = [:]
                    gotmp.put('term',goline[0])
                    gotmp.put('process',goline[1][0])
                    gotmp.put('description',goline[1][2..-1])
                    gotmp.put('evidence',goline[2][0..2])
                    gotmp.put('class',goline[2][4..-1])
                    prot['GO'].add(gotmp)
		}else if(line.startsWith('//')){
//                    if(false){
                    if(reviewed){
			println '\n' + ac + '\t'+reviewed
//			ac.each(){
//				if(imap.containsKey(it)){
//					println imap[it]
//				}
//			}
//			println reviewed
			println 'Name:\t'+prot['ProtName']
                        if(prot.containsKey('Flags')){println 'Flags:\t'+prot['Flags']}
                        prot['AltName'].each(){
                            println 'AltName:\t'+it
                        }
                        prot['Contains'].each(){cont->
                            println 'Contains:\t'+cont['ProtName']
                            cont['AltName'].each(){
                                println '\t\tAltName:\t'+it
                            }
                        }
                        prot['Includes'].each(){incl->
                            println 'Includes:\t'+incl['ProtName']
                            incl['AltName'].each(){
                                println '\t\tAltName:\t'+it
                            }
                        }
                        prot['GN'].each(){g->
                            println 'Gene Names: '+g['name']+'\t'+g['Synonym']
                        }
                        prot['GO'].each(){g->
                            println 'GO:\t'+g
                        }
                        
                    }
                        ac = []
			prot = [:]
                        curEle = [:]
                        curProt = prot
                        def ant = []
                        curProt.put('AltName', ant)
                        con = []
                        curProt.put('Contains', con)
                        inc = []
                        curProt.put('Includes', inc)
                        go = []
                        curProt.put('GO', go)
                        shor = []
                        curEle.put('Short', an)
                        ec = []
                        curEle.put('EC', ec)
                        gene = []
                        curProt.put('GN', gene)
                        skip = false
		}
	}
}